package com.trkj.taoruiyun;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.trkj.base.BaseFragment;
import com.trkj.share.CodeFragment;
import com.trkj.share.HaveARestFragment;
import com.trkj.share.ItFragment;
import com.trkj.share.TechnologyFragment;

/**
 * Created by LiZanhong on 2015/06/15.
 */
public class ShareFragment extends BaseFragment{
    private View shareView;

    private Fragment[] fragments = {
            new TechnologyFragment(),
            new CodeFragment(),
            new ItFragment(),
            new HaveARestFragment()
    };

    private ViewPager viewPager;
    private TextView[] shareTextView = new TextView[4];
    private ImageView line;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (shareView == null) {
            shareView = inflater.inflate(R.layout.fragment_share, container, false);
        }

        findViewsById();
        addListener();
        viewPager.setAdapter(new ShareAdapter(getChildFragmentManager()));
        line.getLayoutParams().width = getScreenSize().getWidth() / shareTextView.length;
        return shareView;
    }

    protected void findViewsById() {
        viewPager = (ViewPager) shareView.findViewById(R.id.share_viewpager);
        shareTextView[0] = (TextView) shareView.findViewById(R.id.share_technology);
        shareTextView[1] = (TextView) shareView.findViewById(R.id.share_code);
        shareTextView[2] = (TextView) shareView.findViewById(R.id.share_it);
        shareTextView[3] = (TextView) shareView.findViewById(R.id.share_havearest);
        line = (ImageView) shareView.findViewById(R.id.line);
    }

    protected void addListener(){
        if(viewPager != null){
            viewPager.setOnPageChangeListener(new OnSharePageChangeListener());
        }

        if(shareTextView != null){
            View.OnClickListener clickListener = new ShareLabelClickListener();
            for(int i = 0; i < shareTextView.length; i ++){
                shareTextView[i].setOnClickListener(clickListener);
            }
        }
    }

    class ShareAdapter extends FragmentPagerAdapter {
        public ShareAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }
    }

    class OnSharePageChangeListener implements ViewPager.OnPageChangeListener {
        private int previousPosition;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            for(int i = 0; i < shareTextView.length; i ++){
                shareTextView[i].setTextColor(getColor(R.color.normal_unselected));
            }

            shareTextView[position].setTextColor(getColor(R.color.normal_selected));

            TranslateAnimation anim = new TranslateAnimation(
                    previousPosition,
                    getScreenSize().getWidth() / shareTextView.length * position,
                    0, 0);
            anim.setDuration(200);
            anim.setFillAfter(true);
            anim.setInterpolator(new AccelerateDecelerateInterpolator());
            line.startAnimation(anim);
            previousPosition = getScreenSize().getWidth() / shareTextView.length * position;

            Log.d("previousPosition", String.valueOf(previousPosition));
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    class ShareLabelClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.share_technology:
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.share_code:
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.share_it:
                    viewPager.setCurrentItem(2);
                    break;
                case R.id.share_havearest:
                    viewPager.setCurrentItem(3);
                    break;
            }
        }
    }
}
